function isEmpty(obj) {
    if (obj == undefined) { // 包含 null 和 undefined
        return true;
    } else if (typeof obj === "string" || Array.isArray(obj)) {
        return obj.length == 0;
    } else {
        return false;
    }
}

function querys(str){
    let obj = {};
    let arr = str.split("&");
    for(let str of arr) {
        let t = str.split("=");
        obj[t[0]] = t[1];
    }
    return obj;
}

function guid(){
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        let r = Math.random() * 16 | 0,
            v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

function uuid(){
    let s = [];
    let hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    return s.join("");
}


function formatDate(date = new Date(), fmt = "yyyy-MM-dd HH:mm:ss:S") {
    let opt = {
            "M+" : date.getMonth()+1,                 //月份
            "d+" : date.getDate(),                    //日
            "h+" : date.getHours()%12, 				  //小时   12小时制
            "H+" : date.getHours(),                   //小时   24小时制
            "m+" : date.getMinutes(),                 //分
            "s+" : date.getSeconds(),                 //秒
            "q+" : Math.floor((date.getMonth()+3)/3), //季度
            "S"  : date.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(let k in opt){
        if(new RegExp("("+ k +")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (opt[k]) : (("00"+ opt[k]).substr((""+ opt[k]).length)));
        }
    }
    return fmt;
}

/**
 * 随机一项在最小(min)与最大(max)之间的数值，默认不包含最大值(calc=0),包含最大值(calc=1)
 * @param {number} min 最小值
 * @param {number} max 最大值
 * @param {number} calc 计算差值
 * @returns {number} 随机一项数值
 */
function randomInt(min = 0, max = 100, calc = 0) {
    min = Math.ceil(min);
    max = Math.floor(max);
    calc = Math.round(calc);
    return Math.floor(Math.random() * (max - min + calc)) + min;
}

/**
 * 随机一项在数组中的项（支持字符串）
 * @param {array} array 数组
 * @returns {object}
 */
function randomEle(array) {
    if(Array.isArray(array) || typeof(array) === "string") {
        let length = array.length;
        if(length != 0){
            let index = randomInt(0, length);
            return array[index];
        }
        return;
    } else {
        return;
    }
    
}


/**
 * 分组
 * @param {array} array 
 * @param {string} key 
 * @param {object} result 
 * @return {object} result
 */
function groupBy(array, key, result = {}) {
    return array.reduce((result, currentValue) => {
        (result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);
        return result;
    }, result);
}

function converTo(input, output) {
    return Object.keys(input).reduce((i, k) => {
        input[k].forEach(key => i[key] = k)
        return i;
    }, output || {});
}


module.exports = { isEmpty, querys, guid, uuid, formatDate, randomInt, randomEle, groupBy, converTo };